<template>
  <p style="text-align: right; padding: 5px">
    <a-button @click="_exportExcel" type="primary" link
      ><template #icon>
        <file-excel-outlined />
      </template>
      导出</a-button
    >
  </p>
  <table v-for="item in list">
    <tr>
      <td rowspan="2" style="width: 50px">序号</td>
      <td rowspan="2" style="width: 200px">{{ item.title }}工序排放源</td>
      <td colspan="2">活动数据/m</td>
      <td colspan="3">因子指标</td>
      <td colspan="3">船舶及海洋工程装备造修企业碳排放核算指南</td>
    </tr>
    <tr>
      <td>工序耗能标定值</td>
      <td>单位</td>
      <td>碳排因子</td>
      <td>单位</td>
      <td>工序耗能碳排放量(kgCO2)</td>
      <td>碳排因子</td>
      <td>单位</td>
      <td>工序耗能碳排放量(kgCO2)</td>
    </tr>
    <template v-for="child in item.children">
      <tr v-for="(o, i) in child.data">
        <td>{{ i + 1 }}</td>
        <td>{{ o.name }}</td>
        <td>{{ o.value.toFixed(4) }}</td>
        <td>{{ o.unit }}</td>
        <td>{{ o.szRatio.toFixed(4) }}</td>
        <td>kgCO2/{{ o.unit }}</td>
        <td>{{ (o.value * o.szRatio).toFixed(4) }}</td>
        <td>{{ o.ratio.toFixed(4) }}</td>
        <td>kgCO2/{{ o.unit }}</td>
        <td>{{ (o.value * o.ratio).toFixed(4) }}</td>
      </tr>
      <tr style="background-color: rgb(255, 255, 0)">
        <td colspan="4">{{ child.title }}碳标定值(kgCO2/m)</td>
        <td colspan="3">
          {{ _.sum(_.map(child.data, (o) => o.value * o.szRatio)).toFixed(4) }}
        </td>
        <td colspan="3">
          {{ _.sum(_.map(child.data, (o) => o.value * o.ratio)).toFixed(4) }}
        </td>
      </tr>
    </template>
  </table>
</template>
<script setup>
import {
  reactive,
  ref,
  computed,
  onMounted,
  getCurrentInstance,
  toRaw,
} from "vue";
import _ from "lodash";
import { exportExcel, setCellStyle } from "@/utils/exportExcel.js";

const { proxy } = getCurrentInstance();
const state = reactive({ source: [] });
const props = defineProps(["work"]);
const emits = defineEmits(["onChild"]);

onMounted(async () => {
  const { success, data } = await proxy.$axios.get("/calibration/stat");
  if (success) {
    state.source = data;
  }
});

const translate = {
  0: "船体下料工序",
  1: "船体割旧工序",
  2: "船体焊接工序",
  "3-0": "机电法兰焊接工序",
  "3-1": "机电弯头管子焊接工序",
  "4-0": "喷漆房分段喷漆工序",
  "4-1": "舱室及船壳喷漆工序",
  "5-0": "打砂房分段打砂工序",
  "5-1": "舱室打砂工序",
  "6-0": "超水清洁点打工序",
  "6-1": "超水清洁工序",
  "6-2": "超水除锈工序",
  "6-3": "舱盖超水除锈工序",
};

const list = computed(() => {
  const array = [];
  const childWork = translate[props.work];
  const data = _.filter(state.source, { childWork });
  const work = _.groupBy(data, "work");
  _.each(work, (v, k) => {
    const childWork = _.groupBy(v, "childWork");
    const children = _.map(childWork, (v1, k1) => {
      return { title: k1, data: v1 };
    });
    array.push({ title: k, children });
  });
  return array;
});

const _exportExcel = () => {
  exportExcel(`标定研究数据`, `标定研究数据`, 10, (worksheet) => {
    let i = 2;
    _.each(list.value, (item) => {
      const head = [];

      const a2 = worksheet.getCell(`A${i}`);
      worksheet.mergeCells(`A${i}:A${i + 1}`);
      a2.value = "序号";
      head.push(a2);

      const b2 = worksheet.getCell(`B2`);
      worksheet.mergeCells(`B${i}:B${i + 1}`);
      b2.value = `${item.title}工序排放源`;
      head.push(b2);

      const c2 = worksheet.getCell(`C${i}`);
      worksheet.mergeCells(`C${i}:D${i}`);
      c2.value = "活动数据/m";
      head.push(c2);

      const c3 = worksheet.getCell(`C${i + 1}`);
      c3.value = "工序耗能标定值";
      head.push(c3);

      const d3 = worksheet.getCell(`D${i + 1}`);
      d3.value = "单位";
      head.push(d3);

      const e2 = worksheet.getCell(`E${i}`);
      worksheet.mergeCells(`E${i}:G${i}`);
      e2.value = "因子指标";
      head.push(e2);

      const e3 = worksheet.getCell(`E${i + 1}`);
      e3.value = "碳排因子";
      head.push(e3);

      const f3 = worksheet.getCell(`F${i + 1}`);
      f3.value = "单位";
      head.push(f3);

      const g3 = worksheet.getCell(`G${i + 1}`);
      g3.value = "工序耗能碳排放量(kgCO2)";
      head.push(g3);

      const h2 = worksheet.getCell(`H${i}`);
      worksheet.mergeCells(`H${i}:J${i}`);
      h2.value = "船舶及海洋工程装备造修企业碳排放核算指南";
      head.push(h2);

      const h3 = worksheet.getCell(`H${i + 1}`);
      h3.value = "碳排因子";
      head.push(h3);

      const i3 = worksheet.getCell(`I${i + 1}`);
      i3.value = "单位";
      head.push(i3);

      const j3 = worksheet.getCell(`J${i + 1}`);
      j3.value = "工序耗能碳排放量(kgCO2)";
      head.push(j3);

      head.forEach((cell) => {
        cell.fill = {
          type: "pattern",
          pattern: "solid",
          fgColor: { argb: "c0c0c0" }, // 设置背景色
        };
        setCellStyle(cell);
      });

      i++;

      _.each(item.children, (child) => {
        _.each(child.data, (column, index) => {
          i++;
          const cellA = worksheet.getCell(`A${i}`);
          cellA.value = index + 1;

          const cellB = worksheet.getCell(`B${i}`);
          cellB.value = column.name;

          const cellC = worksheet.getCell(`C${i}`);
          cellC.value = column.value;

          const cellD = worksheet.getCell(`D${i}`);
          cellD.value = column.unit;

          const cellE = worksheet.getCell(`E${i}`);
          cellE.value = column.szRatio;

          const cellF = worksheet.getCell(`F${i}`);
          cellF.value = `kgCO2/${column.unit}`;

          const cellG = worksheet.getCell(`G${i}`);
          cellG.value = (column.value * column.szRatio).toFixed(4);

          const cellH = worksheet.getCell(`H${i}`);
          cellH.value = column.ratio;

          const cellI = worksheet.getCell(`I${i}`);
          cellI.value = `kgCO2/${column.unit}`;

          const cellJ = worksheet.getCell(`J${i}`);
          cellJ.value = (column.value * column.ratio).toFixed(4);
        });

        i++;
        const t1 = worksheet.getCell(`A${i}`);
        worksheet.mergeCells(`A${i}:D${i}`);
        t1.value = `${child.title}碳标定值(kgCO2/m)`;
        setCellStyle(t1);

        const t2 = worksheet.getCell(`E${i}`);
        worksheet.mergeCells(`E${i}:G${i}`);
        t2.value = parseFloat(
          _.sum(_.map(child.data, (o) => o.value * o.szRatio)).toFixed(4)
        );
        setCellStyle(t2);

        const t3 = worksheet.getCell(`H${i}`);
        worksheet.mergeCells(`H${i}:J${i}`);
        t3.value = parseFloat(
          _.sum(_.map(child.data, (o) => o.value * o.ratio)).toFixed(4)
        );
        setCellStyle(t3);
      });
    });
  });
};
</script>
<style lang="less" scoped>
table {
  width: 100%;

  th,
  td {
    border: 1px solid gray;
    text-align: center;
    line-height: 30px;
    padding: 6px;
    font-size: 1.1em;
    background-color: #141414;
  }
}
</style>
